
import { GridMap } from "./findpath/Grid";
import Unit from "./Unit";

export class Group {
    groupId: number;
    units: Set<Unit> = new Set<Unit>();
    add(unit: Unit) {
        unit.groupId = this.groupId;
        this.units.add(unit);
    }
    update(dt) {
        this.units.forEach((unit) => {
            if (unit.update) {
                unit.update(dt);
            }

        })
    }
}
export default class GameWorld {

    mapNode: cc.Node;
    gridEdge: number = 20;
    groups: Set<Group> = new Set<Group>();

    deathUnits: Set<Unit> = new Set<Unit>();
    init() {

    }
    public static create() {

    }
    checkAllUnit(callBack: (unit: Unit) => boolean) {
        this.groups.forEach((group) => {
            group.units.forEach((unit) => {
                if (callBack(unit))
                    return true;
            })
        })
        return false;
    }
    getOtherGroupUnits(id: number) {
        let units = [];
        this.groups.forEach((group) => {
            if (group.groupId != id) {
                group.units.forEach((unit) => {
                    units.push(unit);
                })

            }
        })
        return units;
    }
    getNearestUnit(unit: Unit): Unit {
        let distance = 0;
        let aimUnit = null;
        this.groups.forEach((group) => {
            if (group.groupId != unit.groupId) {
                group.units.forEach((u) => {
                    if (!u.isDeath()) {
                        let d = u.pos.sub(unit.pos).len();
                        if (distance == 0 || d < distance) {
                            distance = d;
                            aimUnit = u;
                        }
                    }

                })

            }
        });
        return aimUnit;
    }
    addGroup(group: Group) {
        this.groups.add(group);
    }
    addUnitToGroup(unit: Unit, groupId: number) {
        this.groups.forEach((group) => {
            if (group.groupId == groupId) {
                unit.groupId = groupId;
                unit.world = this;
                group.units.add(unit);
            }
        })
    }
    getGroup(groupId: number) {
        let g = null;
        this.groups.forEach((group) => {
            if (group.groupId == groupId) {
                g = group;
            }
        });
        return g;
    }
    removeUint(unit: Unit) {
        let group = this.getGroup(unit.groupId);

        group.units.delete(unit);

    }
    resetUnitOnMap() {
        //GridMap.instance.
        this.groups.forEach((group) => {
            group.units.forEach((unit) => {
                let now = unit.moveComponent.nowPos;
                let aim = unit.moveComponent.aimNode;
                if (aim) {
                    let dn = now.sub(unit.pos).len();
                    let da = aim.position.sub(unit.pos).len();
                    if (dn < da) {
                        GridMap.instance.getNode(now.x, now.y).hasUnit = true;
                        GridMap.instance.getNode(aim.position.x, aim.position.y).hasUnit = false;
                    } else {
                        GridMap.instance.getNode(now.x, now.y).hasUnit = false;
                        GridMap.instance.getNode(aim.position.x, aim.position.y).hasUnit = true;
                    }
                }
            })
        })
    }
    update(dt) {
        // this.resetUnitOnMap();
        this.groups.forEach((group) => {
            group.update(dt);
        })
    }
}